package cn.walls1717.wallsmall.ware.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.walls1717.wallsmall.common.utils.PageUtils;
import cn.walls1717.wallsmall.common.utils.Query;
import cn.walls1717.wallsmall.ware.dao.PurchaseDetailDao;
import cn.walls1717.wallsmall.ware.entity.PurchaseDetailEntity;
import cn.walls1717.wallsmall.ware.service.PurchaseDetailService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity>
    implements PurchaseDetailService {

    /**
     * 分页条件查询
     *
     * @param params 查询条件
     * @return 分页数据
     */
    @Override
    public PageUtils listPagesByConditions(Map<String, Object> params) {
        LambdaQueryWrapper<PurchaseDetailEntity> lqw = new LambdaQueryWrapper<>();
        // 查询条件purchase_id、sku_id
        String key = (String) params.get("key");
        if (!StrUtil.isEmpty(key)) {
            lqw.and(w->w.eq(PurchaseDetailEntity::getPurchaseId, key).or().eq(PurchaseDetailEntity::getSkuId, key));
        }
        // 查询条件status
        String status = (String) params.get("status");
        if (!StrUtil.isEmpty(status)) {
            lqw.eq(PurchaseDetailEntity::getStatus, status);
        }
        // 查询条件ware_id
        String wareId = (String) params.get("wareId");
        if (!StrUtil.isEmpty(wareId) && !StrUtil.equals(wareId, "0")) {
            lqw.eq(PurchaseDetailEntity::getWareId, wareId);
        }
        IPage<PurchaseDetailEntity> page = this.page(new Query<PurchaseDetailEntity>().getPage(params), lqw);

        return new PageUtils(page);
    }

    /**
     * 根据采购单id查找采购需求
     *
     * @param purchaseId 采购单id
     * @return 采购详情集合
     */
    @Override
    public List<PurchaseDetailEntity> listPurchaseDetailsByPurchaseId(Long purchaseId) {
        return lambdaQuery().eq(PurchaseDetailEntity::getPurchaseId, purchaseId).list();
    }
}